﻿using GoEasy.Bussiness;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace GoEasy
{
    public delegate void ChangedEventHandler(object sender, EventArgs e, dynamic Respone);
    class RequestCommonFunction
    {
        private static ManualResetEvent allDone = new ManualResetEvent(false);
        public event ChangedEventHandler RequestDone;
        public void Request(string url, string Parameters)
        {
            WebClient webClient = new WebClient();
            webClient.UploadStringAsync(new Uri(url), Parameters);
            webClient.UploadStringCompleted += webClient_DownloadStringCompleted;
        }
        public void webClient_DownloadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            dynamic root="{}";
            try
            {
                if (!string.IsNullOrEmpty(e.Result))
                {
                    //Parse JSON result as POCO 
                     root= JsonConvert.DeserializeObject(e.Result);
                    var s = root.Result;
                    if (RequestDone != null)
                        RequestDone(this, EventArgs.Empty, root);
                }
            }
            catch (Exception ex)
            {
                ErrorManager.RequestFailError(ex);
            }
            finally
            {
                
            }

        }

    }
}
